//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;

namespace OpenLS.Spreadsheet.Extensibility
{
    public class CompositeArgumentConverter : IParameterConverter
    {
        public CompositeArgumentConverter(IEnumerable<IParameterConverter> c)
        {
            converters = c;
        }
        public static CompositeArgumentConverter ToDoubleConverter()
        {
            List<IParameterConverter> l = new List<IParameterConverter>
                                              {
                                                  new TreatBooleansAsDoubleAttribute(),
                                                  new AllowStringToDoubleConversionAttribute()
                                              };
            CompositeArgumentConverter converter = new CompositeArgumentConverter(l);
            return converter;
        }

        readonly IEnumerable<IParameterConverter> converters;
        #region IArgumentConverter Members

        public void Convert(ConversionData data)
        {
           /* foreach (IParameterConverter c in converters)
            {
                if (c is AccessCellValueAttribute)
                {
                    if (data.Value is ICell)
                    {
                        data.Dereference();
                    }
                    break;
                }
            }
            if (data.Value != null && data.TargetType.IsAssignableFrom(data.Value.GetType()))
            {
                data.Result = data.Value;
                return;
            }*/
            foreach (IParameterConverter c in converters)
            {
                c.Convert(data);
                if (data.Successful)
                {
                    return;
                }
                if (data.Ignore || data.Error != null)
                {
                    //data.Successful = true;
                    return;
                }
            }
            return;
        }

        public int Priority
        {
            get { return 0; }
        }

        #endregion

        public bool ContainsNoConversion()
        {
            foreach (var converter in converters)
            {
                if (converter is NoConversionAttribute)
                    return true;
            }
            return false;
        }
    }
}